#!/usr/bin/env bash

requirements_cygwin_lib_installed()
{
  cygcheck -c -d | __rvm_grep "^$1" >/dev/null || return $?
}

requirements_cygwin_libs_install()
{
  \typeset __packages_list __manager

  rvm_debug "__manager: detecting administrative permissions"
  net session > /dev/null 2>&1
  if [ $? -eq 0 ];
  then __manager_admin_mode=""
  else __manager_admin_mode="--no-admin"
  fi

  if [[ "${_system_arch}" == "x86_64" ]]
  then __manager="setup-x86_64.exe"
  else __manager="setup-x86.exe"
  fi
  rvm_debug "__manager: ${__manager} ${__manager_admin_mode}"

  __packages_list="$*"
  ${__manager} ${__manager_admin_mode} -q -P "${__packages_list// /,}" || return $?
}

requirements_cygwin_update_system()
{
  true # really???
}

requirements_cygwin_define()
{
  # Verify cygwin package names using:
  # https://cygwin.com/cgi-bin2/package-grep.cgi

  case "$1" in
    (rvm)
      requirements_check bash curl patch gnupg
      ;;

    (jruby*)
      requirements_check make

      if
        is_head_or_disable_binary "$1"
      then
        requirements_check git
        case $( jruby_installation_method  "$1" ) in
          ant)  echo "Need Java JDK and Ant"   ;;
          mvn)  echo "Need Java JDK and Maven" ;;
          mvnw) echo "Need Java JDK"           ;;
        esac
      else
        echo "Need Java"
      fi
      ;;

    (ir*)
      true # should be already there in most cases
      ;;

    (opal)
      echo "Need Node.js"
      ;;

    (*-head)
      requirements_check autoconf git
      requirements_cygwin_define "${1%-head}"
      ;;

    (*)
      requirements_check make autoconf automake bison m4 libtool \
        gcc gcc-core mingw64-i686-gcc-core mingw64-x86_64-gcc-core \
        libiconv zlib zlib-devel \
        openssl libssl-devel \
        libcrypt-devel libcrypt0 \
        libyaml-devel libyaml0_2 \
        libffi-devel \
        libreadline-devel \
        sqlite3 \
        patch \
        gnupg
      ;;
  esac
}
